//package org.jeecg.modules.middleware.quartz.job.jgupload;
//
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.collections4.CollectionUtils;
//import org.apache.commons.collections4.MapUtils;
//import org.apache.commons.lang.StringUtils;
//import org.jeecg.common.constant.enums.ReportPlatformEnum;
//import org.jeecg.common.util.DateUtils;
//import org.jeecg.modules.third.jg.jss.report.cfccapitalflow.service.CfcCapitalflowService;
//import org.jeecg.modules.third.jg.jss.report.cfcdriverinfo.service.CfcDriverinfoService;
//import org.jeecg.modules.third.jg.jss.report.cfcnocarcarrierplatforminfo.service.CfcNocarcarrierplatforminfoService;
//import org.jeecg.modules.third.jg.jss.report.cfcshipperinfo.service.CfcShipperinfoService;
//import org.jeecg.modules.third.jg.jss.report.cfcshippingnoteinfo.service.CfcShippingnoteinfoService;
//import org.jeecg.modules.third.jg.jss.report.cfcsourcegoodsinfo.service.CfcSourcegoodsinfoService;
//import org.jeecg.modules.third.jg.jss.report.cfcvehicleinfo.service.CfcVehicleinfoService;
//import org.jeecg.modules.wlhy.bill.entity.HyShippingNoteBill;
//import org.jeecg.modules.wlhy.bill.service.IHyShippingNoteBillService;
//import org.jeecg.modules.wlhy.company.entity.HyCompany;
//import org.jeecg.modules.wlhy.company.entity.HyCompanyConfig;
//import org.jeecg.modules.wlhy.company.service.IHyCompanyConfigService;
//import org.jeecg.modules.wlhy.company.service.IHyCompanyService;
//import org.jeecg.modules.wlhy.customer.entity.HyCustomer;
//import org.jeecg.modules.wlhy.customer.entity.HyCustomerCompany;
//import org.jeecg.modules.wlhy.customer.service.IHyCustomerCompanyService;
//import org.jeecg.modules.wlhy.customer.service.impl.HyCustomerServiceImpl;
//import org.jeecg.modules.wlhy.hyconfig.entity.HyConfig;
//import org.jeecg.modules.wlhy.hydriver.entity.HyDriver;
//import org.jeecg.modules.wlhy.hydriver.entity.HyDriverReport;
//import org.jeecg.modules.wlhy.hydriver.service.IHyDriverReportService;
//import org.jeecg.modules.wlhy.hydriver.service.impl.HyDriverServiceImpl;
//import org.jeecg.modules.wlhy.hyvehicle.entity.HyVehicle;
//import org.jeecg.modules.wlhy.hyvehicle.entity.HyVehicleReport;
//import org.jeecg.modules.wlhy.hyvehicle.entity.HyVehicleType;
//import org.jeecg.modules.wlhy.hyvehicle.service.IHyVehicleReportService;
//import org.jeecg.modules.wlhy.hyvehicle.service.impl.HyVehicleServiceImpl;
//import org.jeecg.modules.wlhy.order.entity.HyOrder;
//import org.jeecg.modules.wlhy.order.service.impl.HyOrderServiceImpl;
//import org.jeecg.modules.wlhy.shipping.entity.HyShippingNote;
//import org.jeecg.modules.wlhy.shipping.service.IHyShippingNoteService;
//import org.jeecg.modules.wlhy.utils.HyConfigUtils;
//import org.quartz.*;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//
//import javax.annotation.Resource;
//import java.math.BigDecimal;
//import java.text.SimpleDateFormat;
//import java.util.Calendar;
//import java.util.*;
//import java.util.function.Function;
//import java.util.stream.Collectors;
//
///**
// * @Description: 江苏省监管平台
// * @author: duanlikao
// * @date: 2021年11月24日 11:43
// */
//
//@PersistJobDataAfterExecution
//@DisallowConcurrentExecution
//@Slf4j
//public class JssReportService implements Job {
//
//    /**
//     * 网络货运平台信息
//     */
//    @Resource
//    private CfcNocarcarrierplatforminfoService nocarcarrierplatforminfoService;
//
//    /**
//     * 司机信息
//     */
//    @Resource
//    private CfcDriverinfoService driverinfoService;
//
//    /**
//     * 车辆信息
//     */
//    @Resource
//    private CfcVehicleinfoService vehicleinfoService;
//
//    /**
//     * 托运人/货主信息
//     */
//    @Resource
//    private CfcShipperinfoService shipperinfoService;
//
//    /**
//     * 货源信息信息
//     */
//    @Resource
//    private CfcSourcegoodsinfoService sourcegoodsinfoService;
//    /**
//     * 运单信息
//     */
//    @Resource
//    private CfcShippingnoteinfoService shippingnoteinfoService;
//
//    /**
//     * 资金流水信息
//     */
//    @Resource
//    private CfcCapitalflowService capitalflowService;
//
//
//    @Autowired
//    private IHyShippingNoteService shippingNoteService;
//
//    @Autowired
//    private IHyCompanyService companyService;
//
//    @Autowired
//    private IHyShippingNoteBillService shippingNoteBillService;
//
//    @Autowired
//    private HyOrderServiceImpl orderService;
//
//    @Autowired
//    private HyCustomerServiceImpl customerService;
//
//    @Autowired
//    private HyVehicleServiceImpl vehicleService;
//
//
//    @Autowired
//    private HyDriverServiceImpl driverService;
//
//    @Autowired
//    private IHyCompanyConfigService companyConfigService;
//
//    /**
//     * 托运人合同信息
//     */
//    @Autowired private IHyCustomerCompanyService customerCompanyService;
//
//
//    @Resource
//    private IHyDriverReportService hyDriverReportService ;
//
//    @Resource
//    private IHyVehicleReportService vehicleReportService;
//
//
//    private static final Logger LOGGER = LoggerFactory.getLogger(JssReportService.class);
//
//    public SimpleDateFormat now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//
//
//    //@Scheduled(cron = "0 30 3 * * ?")//每天的3:30:00执行
////    @Scheduled(cron =  "0 */5 * * * ? ")//每5分中执行一次
////    public  void  autoUploadJsShippingNot(){
//    @Override
//    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
//        HyConfig systemConfig = HyConfigUtils.getConfig();
//        if(systemConfig == null || !systemConfig.getJsReportIzUse()){
//            log.warn("江苏上报服务暂未开启,请联系管理。");
//            return;
//        }
//
//        List<HyCompanyConfig> companyConfigs = companyConfigService.list(new QueryWrapper<HyCompanyConfig>()
//                .eq("report_platform_type", ReportPlatformEnum.REPORT_PLATFORM_JS.getCode())
//                .eq("auto_report",1)
//        );
//        if(CollectionUtils.isEmpty(companyConfigs)) {
//            log.warn("JssReportService autoUploadJsShippingNot: 没有分公司配置自动上报 !" );
//            return;
//        }
//        Long start = System.currentTimeMillis();
//        log.info("JssReportService autoUploadJsShippingNot:执行运单上报数据开始" + now.format(new Date()));
//
//        Calendar calendar = new GregorianCalendar();
//        calendar.setTime(new Date());
//        calendar.add(Calendar.DATE, -60);// 把日期往后增加一天.整数往后推,负数往前移动
//        Date lasDate = calendar.getTime(); // 这个时间就是日期往后推一天的结果
//        String startDateString =  DateUtils.date2Str(lasDate, DateUtils.date_sdf.get());
//        String endDateString =  DateUtils.dateTimeToString(new Date());
//
//        for (HyCompanyConfig companyConfig : companyConfigs) {
//
//            HyCompany hyCompanyDb =  companyService.getById(companyConfig.getCompanyId());
//            this.upCompany(hyCompanyDb);
//
//            List<HyShippingNote> newShippingNotes = new ArrayList<>();
//            List<HyShippingNoteBill> shippingNoteBills = new ArrayList<>();
//
//
//            QueryWrapper<HyShippingNote> queryWrapper = new QueryWrapper();
//
//            queryWrapper.eq("shipping_pay_status","6");
//            queryWrapper.eq("company_id",companyConfig.getCompanyId());
//            queryWrapper.ne("upload_status","1");
//            //queryWrapper.eq("order_mode","1");
//            queryWrapper.between("pay_dt",startDateString+" 00:00:00", endDateString );
//            queryWrapper.orderByAsc("pay_dt");
//
//
//            List<HyShippingNote> shippingNotes = shippingNoteService.list(queryWrapper);
//            if(CollectionUtils.isEmpty(shippingNotes)) {
//                log.warn("JssReportService autoUploadJsShippingNot: no data handling !" );
//                continue;
//            }
//
//
//            Map <String , String> driverIdAndvehicleIdMaps  = shippingNotes.stream()
//                    .collect(Collectors.toMap(HyShippingNote::getDriverId, HyShippingNote::getVehicleId,  (key1, key2) -> key2));
//
//            Map<String , String> vehicleIdAnddriverIdMaps  = shippingNotes.stream()
//                    .collect(Collectors.toMap( HyShippingNote::getVehicleId,HyShippingNote::getDriverId,  (key1, key2) -> key2));
//
//            //查询车辆
//            List<String> vehicleIds =   shippingNotes.stream().map(
//                    item ->item.getVehicleId()
//            ).distinct().collect(Collectors.toList());
//
//            List<HyVehicle>  vehicleList = vehicleService.listByIds(vehicleIds);
//
//            QueryWrapper<HyVehicleReport> queryWrapperVehicle = new QueryWrapper();
//            queryWrapperVehicle.in("id" ,vehicleIds) ;
//            queryWrapperVehicle.in("report_platform_type" ,ReportPlatformEnum.REPORT_PLATFORM_JS.getCode()) ;
//            List<HyVehicleReport>  hyVehicleReports =  vehicleReportService.list(queryWrapperVehicle);
//
//            Map<String , HyVehicle> vehicleMaps = vehicleList.stream().collect(Collectors.toMap(HyVehicle::getId, Function
//                    .identity(),  (key1, key2) -> key2));
//
//            Map<String , HyVehicleReport> vehicleReportMaps = hyVehicleReports.stream().collect(Collectors.toMap(HyVehicleReport::getId, Function
//                    .identity(),  (key1, key2) -> key2));
//
//            if(CollectionUtils.isNotEmpty(vehicleList)){
//                List<HyVehicleReport>  newVehicleLies = new ArrayList<>();
//                for (HyVehicle  hyVehicle :vehicleList){
//                    HyVehicleReport hyVehicleReport = vehicleReportMaps.get(hyVehicle.getId()) ;
//                    if(hyVehicleReport == null){
//                        this.saveVehicleinfo(vehicleIdAnddriverIdMaps, newVehicleLies, hyVehicle, hyVehicleReport);
//                    }
//                    if(hyVehicleReport != null && !"1".equals(hyVehicleReport.getVehicleUploadStatus()) ){
//                        this.saveVehicleinfo(vehicleIdAnddriverIdMaps, newVehicleLies, hyVehicle, hyVehicleReport);
//                    }
//                }
//                if(CollectionUtils.isNotEmpty(newVehicleLies)) {
//                    vehicleReportService.saveOrUpdateBatch(newVehicleLies);
//                }
//            }
//
//
//            //查询司机
//            List<String> driverIds =   shippingNotes.stream().map(
//                    item ->item.getDriverId()
//            ).distinct().collect(Collectors.toList());
//
//            List<HyDriver>  driverList = driverService.listByIds(driverIds);
//
//            QueryWrapper<HyDriverReport> queryWrapperDriver = new QueryWrapper();
//            queryWrapperDriver.in("id" ,driverIds) ;
//            queryWrapperDriver.in("report_platform_type" ,ReportPlatformEnum.REPORT_PLATFORM_JS.getCode()) ;
//            List<HyDriverReport>  hyDriverReports =  hyDriverReportService.list(queryWrapperDriver);
//
//            Map<String , HyDriverReport> driverReportMaps = hyDriverReports.stream().collect(Collectors.toMap(HyDriverReport::getId, Function
//                    .identity(),  (key1, key2) -> key2));
//
//            Map<String , HyDriver> driverMap  =  driverList.stream().collect(Collectors.toMap(HyDriver::getId, Function.identity(),  (key1, key2) -> key2));
//
//            if(CollectionUtils.isNotEmpty(driverList)){
//                List<HyDriverReport>  newDrivers = new ArrayList<>();
//
//                for (HyDriver hyDriver:driverList){
//                    HyDriverReport hyDriverReport = driverReportMaps.get(hyDriver.getId()) ;
//                    if(hyDriverReport == null){
//                        this.saveDriverinfo(driverIdAndvehicleIdMaps, vehicleMaps, newDrivers, hyDriver, hyDriverReport);
//                    }
//                    if(hyDriverReport != null && !"1".equals(hyDriverReport.getDriverUploadStatus()) ){
//                        this.saveDriverinfo(driverIdAndvehicleIdMaps, vehicleMaps, newDrivers, hyDriver, hyDriverReport);
//                    }
//                }
//                if(CollectionUtils.isNotEmpty(newDrivers)) {
//                    hyDriverReportService.saveOrUpdateBatch(newDrivers);
//                }
//
//            }
//
//            //查询货主
//            List<String> customerIds =   shippingNotes.stream().map(
//                    item ->item.getCustomerId()
//            ).distinct().collect(Collectors.toList());
//
//            List<HyCustomer>  customerList = customerService.listByIds(customerIds);
//            Map<String , HyCustomer> customerMap  =  customerList.stream().collect(Collectors.toMap(HyCustomer::getId, Function.identity(),  (key1, key2) -> key2));
//
//
//            //查询货源
//            List<String> orderIds =   shippingNotes.stream().map(
//                    item ->item.getOrderId()
//            ).distinct().collect(Collectors.toList());
//
//            List<HyOrder>  orderList = orderService.listByIds(orderIds);
//            Map<String , HyOrder>  orderMap  =  orderList.stream().collect(Collectors.toMap(HyOrder::getId, Function.identity(),  (key1, key2) -> key2));
//
//            for(HyShippingNote hyShippingNote:shippingNotes){
//                try {
//                    HyCustomer customer = customerMap.get(hyShippingNote.getCustomerId());
//                    HyOrder hyOrder = orderMap.get(hyShippingNote.getOrderId());
//                    HyDriver hyDriver = driverMap.get(hyShippingNote.getDriverId());
//
//                    this.upCustomerInfo(customer);
//
//                    BigDecimal costPayShipping = hyShippingNote.getDriverFee() ;
//                    upOrdeInfo(hyOrder,hyShippingNote.getCustomerId(),hyCompanyDb,costPayShipping);
//
//                    if (shippingnoteinfoService.inserShippingNoteInfo(hyShippingNote ,hyCompanyDb ,hyOrder ,hyDriver)) {
//
//                        hyShippingNote.setUploadDate(new Date());
//                        hyShippingNote.setUploadStatus("1");
//                        hyShippingNote.setReportPlatformType(ReportPlatformEnum.REPORT_PLATFORM_JS.getCode());
//                        newShippingNotes.add(hyShippingNote);
//
//                        QueryWrapper <HyShippingNoteBill> queryWrapperShippingNoteBill = new QueryWrapper();
//                        queryWrapperShippingNoteBill.eq("shipping_id", hyShippingNote.getId());
//                        queryWrapperShippingNoteBill.ne("upload_status", "1");
//                        List<HyShippingNoteBill> hyShippingNoteBills = shippingNoteBillService.list(queryWrapperShippingNoteBill);
//                        if(capitalflowService.inserCapitalflow(hyShippingNoteBills ,hyShippingNote)){
//                            for(HyShippingNoteBill hyShippingNoteBill:hyShippingNoteBills){
//                                hyShippingNoteBill.setUploadDate(new Date());
//                                hyShippingNoteBill.setUploadStatus("1");
//                                hyShippingNoteBill.setReportPlatformType(ReportPlatformEnum.REPORT_PLATFORM_JS.getCode());
//                                shippingNoteBills.add(hyShippingNoteBill);
//                            }
//                        }
//                    }
//
//                }catch (Exception e){
//                    log.error("JssReportService autoUploadJsShippingNot:Exception : {}" ,e.getMessage());
//                    continue;
//                }
//            }
//            if(CollectionUtils.isNotEmpty(newShippingNotes)) {
//                shippingNoteService.updateBatchById(newShippingNotes);
//            }
//            if(CollectionUtils.isNotEmpty(shippingNoteBills)) {
//                shippingNoteBillService.updateBatchById(shippingNoteBills) ;
//            }
//        }
//
//        log.info("JssReportService autoUploadJsShippingNot:执行运单上报数据结束,耗时:"+(System.currentTimeMillis()-start)+"毫秒----");
//        return;
//    }
//
//    private void saveDriverinfo(Map<String, String> driverIdAndvehicleIdMaps,
//                                Map<String, HyVehicle> vehicleMaps,
//                                List<HyDriverReport> newDrivers,
//                                HyDriver hyDriver,
//                                HyDriverReport hyDriverReport) {
//
//        hyDriverReport.setId(hyDriver.getId());
//        hyDriverReport.setDriverId(hyDriver.getId());
//        hyDriverReport.setReportPlatformType(ReportPlatformEnum.REPORT_PLATFORM_JS.getCode());
//        hyDriverReport.setUploadDate( new Date() );
//
//        //人员从业资格证验证
//        if(driverService.checkQualificationV2(hyDriver)){
//            String vid =  MapUtils.isNotEmpty(driverIdAndvehicleIdMaps) && driverIdAndvehicleIdMaps.get(hyDriver.getId()) != null  ?
//                    driverIdAndvehicleIdMaps.get(hyDriver.getId()) : StringUtils.EMPTY;
//            if(StringUtils.isNotEmpty(vid)){
//                HyVehicle vehicle =  vehicleMaps.get(vid);
//                if(driverinfoService.addDriverinfo(hyDriver,vehicle)){
//                    hyDriverReport.setDriverUploadStatus( "1");
//                    hyDriverReport.setDriverReportFailureReason( "");
//                    newDrivers.add(hyDriverReport);
//                }
//            }
//
//        }else{
//            hyDriverReport.setDriverUploadStatus( "2");
//            hyDriverReport.setDriverReportFailureReason( "人员从业资格证验证号不正确或者已过期，请修改后重新上传！");
//            newDrivers.add(hyDriverReport);
//        }
//    }
//
//    private void saveVehicleinfo(Map<String, String> vehicleIdAnddriverIdMaps,
//                                 List<HyVehicleReport> newVehicleLies,
//                                 HyVehicle hyVehicle,
//                                 HyVehicleReport hyVehicleReport) {
//        if(vehicleService.checkRTCNoV2(hyVehicle)){
//            HyVehicleType vehicleType = vehicleService.getHyVehicleType(hyVehicle.getVehicleType());
//            hyVehicle.setVehicleType(vehicleType != null ?   vehicleType.getValue() : "Q11");
//            hyVehicle.setCarrierId( vehicleIdAnddriverIdMaps.get(hyVehicle.getId()) );
//            if(vehicleinfoService.addVehicleinfo(hyVehicle)) {
//                hyVehicleReport.setId(hyVehicle.getId());
//                hyVehicleReport.setVehicleId(hyVehicle.getId());
//                hyVehicleReport.setReportPlatformType(ReportPlatformEnum.REPORT_PLATFORM_JS.getCode());
//                hyVehicleReport.setVehicleUploadStatus( "1");
//                hyVehicleReport.setUploadDate( new Date() );
//                hyVehicleReport.setVehicleReportFailureReason("");
//                newVehicleLies.add(hyVehicleReport);
//            }
//        }else{
//            hyVehicleReport.setId(hyVehicle.getId());
//            hyVehicleReport.setVehicleId(hyVehicle.getId());
//            hyVehicleReport.setReportPlatformType(ReportPlatformEnum.REPORT_PLATFORM_JS.getCode());
//            hyVehicleReport.setVehicleUploadStatus( "2");
//            hyVehicleReport.setUploadDate( new Date() );
//            hyVehicleReport.setVehicleReportFailureReason("车辆道路运输证号不正确或者已过期，请修改后重新上传！");
//            newVehicleLies.add(hyVehicleReport);
//        }
//    }
//
//    /**
//     * 货主
//     * @param customer
//     */
//    private void upCustomerInfo(HyCustomer customer){
//        shipperinfoService.addShipperinfo( customer );
//    }
//
//    /**
//     * 货源
//     * @param hyOrde
//     * @param customerId
//     * @param hyCompany
//     */
//    private void upOrdeInfo(HyOrder hyOrde , String customerId, HyCompany hyCompany , BigDecimal costPayShipping){
//
//
//        QueryWrapper<HyCustomerCompany> queryWrapperCustomerCompany = new QueryWrapper();
//        queryWrapperCustomerCompany.eq("customer_id",customerId );
//        //查询货主签约
//        HyCustomerCompany customerCompany = customerCompanyService.getOne(queryWrapperCustomerCompany);
//
//        sourcegoodsinfoService.insertSourcegoodsinfo(  hyOrde, customerCompany, hyCompany,costPayShipping )  ;
//    }
//
//    /**
//     * 企业信息
//     * @param hyCompany
//     */
//   private void upCompany(HyCompany hyCompany){
//      nocarcarrierplatforminfoService.addCompany(hyCompany )  ;
//   }
//
//
//
////    @Resource
////    private UsertMapper userMapper;
//
////    @CurDataSource(name = DataSourceNames.SECOND)
////    public List<Usert> getAllUsers() {
////        return userMapper.getAllUsers();
////    }
////
////    @CurDataSource(name = DataSourceNames.SECOND)
////    public Usert addUser(Usert user) {
////        SimpleDateFormat form = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
////        user.setCreatedTime( form.format(new Date()) );
////        userMapper.addUser( user );
////        return user;
////    }
////
////    @CurDataSource(name = DataSourceNames.SECOND)
////    public int deleteUser(Usert user) {
////        return userMapper.deleteUser( user );
////    }
//
//}
